﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.mIE.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mIE.UI
{
    public partial class Frm_IE_Dwg_LockTask : frmBaseChild
    {
        public Frm_IE_Dwg_LockTask()
        {
            InitializeComponent();
        }

        private void Frm_IE_Dwg_LockTask_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            InitProductItem();
        }
        //产品类型初始化
        public void InitProductItem()
        {
            DataTable dt = BaseFactory<IE_Part_TaskList>.Instance.GetProductItem();
            this.txtProductItem.Properties.Items.Clear();
            if (dt.Rows.Count > 0)
            {
                string[] list = dt.Rows[0]["FIELD_VALUE_OPTION"].ToString().Split(',');
                foreach (var item in list)
                {
                    this.txtProductItem.Properties.Items.Add(item);
                }
            }
        }
        public override void DoView(IButtonInfo sender)
        {
            grid_Main.DataSource = new DataTable();
            string strDWGNO = txtDwgNo.Text.Trim();
            string strTYPE = txtProductItem.Text.Trim();
            string strPARTNO = txtPartNo.Text.Trim();
            string sql = "";
            if (string.IsNullOrEmpty(strTYPE))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择产品类型！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(strDWGNO) && string.IsNullOrEmpty(strPARTNO))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请输入图纸号码或部件号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            // if (!string.IsNullOrEmpty(strDWGNO) && !strDWGNO.Contains("NIL"))
            if (!strDWGNO.Contains("NIL") && !string.IsNullOrEmpty(strDWGNO) && string.IsNullOrEmpty(strPARTNO))
                sql = @"SELECT distinct[PROD_CATEGORY]
       ,[DWG_NO]
       ,'**********' as [PART_NO]
       ,LOCK_REASON 
  FROM[LamexMESManager].[dbo].[IE_Part_TaskList] T
  where PROD_CATEGORY='" + strTYPE + "' and DWG_NO='" + strDWGNO + "' " +
     "and ISNULL(UPLOAD_FLAG,'')= '' and ISNULL(LOCK_FLAG,'')= ''";
            else if(!strDWGNO.Contains("NIL") && !string.IsNullOrEmpty(strDWGNO) && !string.IsNullOrEmpty(strPARTNO))
                sql = @"SELECT distinct[PROD_CATEGORY]
       ,[DWG_NO]
       ,[PART_NO]
       ,LOCK_REASON 
  FROM[LamexMESManager].[dbo].[IE_Part_TaskList] T
  where PROD_CATEGORY='" + strTYPE + "' and DWG_NO='" + strDWGNO + "' and PART_NO='" + strPARTNO + "' " +
"and ISNULL(UPLOAD_FLAG,'')= '' and ISNULL(LOCK_FLAG,'')= ''";
            else
                sql = @"SELECT distinct[PROD_CATEGORY]
       ,[DWG_NO]
       ,[PART_NO]
       ,LOCK_REASON 
  FROM[LamexMESManager].[dbo].[IE_Part_TaskList] T
  where PROD_CATEGORY='" + strTYPE + "' and PART_NO='" + strPARTNO + "' " +
   "and ISNULL(UPLOAD_FLAG,'')= '' and ISNULL(LOCK_FLAG,'')= ''";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                grid_Main.DataSource = dt;
                // gv_Main.BestFitColumns();
                //this.gv_Main.OptionsView.ColumnAutoWidth = false;
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("查不到任何数据，请确认输入条件是否正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);               
                return;
            }
        }
        public override void DoSave(IButtonInfo sender)
        {
            try
            {
                this.gv_Main.CloseEditor();
                this.gv_Main.UpdateCurrentRow();
                if (gv_Main.Columns.Count > 0)
                {
                    int[] iDWG_Count = gv_Main.GetSelectedRows();
                    if (iDWG_Count.Length <= 0)
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("请选择需要保存的记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    foreach (int item in iDWG_Count)
                    {
                        string strReason = Convert.ToString(gv_Main.GetRowCellValue(item, "LOCK_REASON"));
                        string strDWGNO = Convert.ToString(gv_Main.GetRowCellValue(item, "DWG_NO"));
                        string strPARTNO = Convert.ToString(gv_Main.GetRowCellValue(item, "PART_NO"));
                        string sql_Update = "";
                        if (string.IsNullOrEmpty(strReason))
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("禁用原因不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }                      
                        if (!strDWGNO.Contains("NIL") && !string.IsNullOrEmpty(strDWGNO) && strPARTNO.Contains("*"))
                            sql_Update = @"update [dbo].[IE_Part_TaskList] set LOCK_FLAG='1',LOCK_REASON='" + strReason + "',LOCK_USER='" + Lamex.Common.Loginer.CurrentUser.AccountName + @"'
                where DWG_NO='" + strDWGNO + "'";
                        else
                            sql_Update = @"update [dbo].[IE_Part_TaskList] set LOCK_FLAG='1',LOCK_REASON='" + strReason + "',LOCK_USER='" + Lamex.Common.Loginer.CurrentUser.AccountName + @"'
                where PART_NO='" + strPARTNO + "'";
                        BaseBuildSql.ExecuteNonQuery(sql_Update);

                    }
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    grid_Main.DataSource = new DataTable();
                    txtDwgNo.Text = "";
                    txtPartNo.Text = "";
                    return;
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
